// pages/search/search.js
const {getSearch} = require("../../api/search");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchFind:['全部商品','电视','手机','冰箱','洗衣机','空调','净水器','耳机'],
    hslist:[],
    keyword:'',
    alllist:[],
    classelist:[],
    goodslist:[],
    flag:false,
    checktype:0,
    delkeyword:false,
    empty:false,
    typeIndex:0,
    checkedflag:true,
    loading:false,
  },
  //input输入框
  wordFun:function(e){
    let value = e.detail.value;
    if (this.data.keyword=='') {
      this.data.flag=false
      this.data.classelist=[]
      this.data.goodslist=[]
      this.data.delkeyword=false
      this.data.checktype=0
      this.setData(this.data)
    }else{
      this.data.delkeyword=true
    }
    this.setData({
      keyword:value,
      delkeyword: this.data.delkeyword
    })
      
  },
  // 搜索
  search(){
    // console.log(this.data.keyword);
    this.data.loading=true
    this.data.flag=true
    this.setData(this.data)
   if (this.data.keyword) {
    this.getSearchFun(this.data.keyword,this.data.checktype)
   }else{
    this.data.loading=false
    this.data.flag=false
    this.setData(this.data)
   }
    let had = this.data.hslist.includes(this.data.keyword)
    if (!had && this.data.keyword!='') {
      this.data.hslist.push(this.data.keyword)
      wx.setStorageSync('hslist',this.data.hslist)
      this.setData(this.data.hslist)
    }
  },
  // 一键清除
  delword(){
    this.data.keyword=''
    this.data.flag=false
    this.data.delkeyword=false
    this.data.empty=false
    this.data.checktype=0
    this.setData(this.data)
  },
  //清除历史记录
  clearhs(){
    this.data.hslist=[]
    wx.setStorageSync('hslist',this.data.hslist)
    this.setData(this.data)
  },
  //选择类型
  changeType(event){
    let index = event.currentTarget.dataset.id
    this.data.checktype=index
    this.data.typeIndex=0
    this.setData(this.data)
    this.getSearchFun(this.data.keyword,this.data.checktype)

  },
  //综合、销量、价格
  checkedType(e){
    // console.log(e);
    let index = e.currentTarget.dataset.id
    this.data.typeIndex=index
    if (this.data.typeIndex==0) {
      this.getSearchFun(this.data.keyword,this.data.checktype)
    }
    if (this.data.typeIndex==1) {
      this.data.goodslist= this.data.goodslist.sort((a,b)=>{return b.body.sort-a.body.sort})
    }
    if (this.data.typeIndex==2) {
      this.data.checkedflag=!this.data.checkedflag
      if (this.data.checkedflag) {     
       this.data.goodslist= this.data.goodslist.sort((a,b)=>{return b.body.price-a.body.price})
      }else{
       this.data.goodslist= this.data.goodslist.sort((a,b)=>{return a.body.price-b.body.price})
      }
    }
    this.setData(this.data)
  },
  //历史搜索
  changeWord(event){
      let index = event.target.dataset.id
      let word = this.data.hslist[index]
      this.data.delkeyword=true
      this.data.keyword=word
      this.setData(this.data)    
  },
  //搜索发现
  selectFWord(event){
    let index = event.target.dataset.id
    let word = this.data.searchFind[index]
    this.data.delkeyword=true
    this.data.keyword=word
    this.setData(this.data)    
  },
  //获取搜索数据
  getSearchFun(word,index){
    getSearch({query:word}).then(data=>{
      console.log(data);
      if (data) {
          this.data.loading=false
      }
      if (this.data.goodslist.length==0) {
        setTimeout(()=>{
          this.data.empty=true
          this.setData(this.data)
        },800)
      }
      let alltemp = data.data
      let classetemp = data.data.classes
      let goodstemp = data.data.list_v2
      let goodlist = goodstemp.filter(item=>item.body.class_sort==classetemp[index].class_sort)
      this.data.flag=true
      this.setData({
        alllist:alltemp,
        classelist:classetemp,
        goodslist:goodlist,
        flag:this.data.flag,
        loading:this.data.loading
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let value = wx.getStorageSync('hslist')
    this.data.hslist=value==''?[]:value
    this.setData(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})